package cn.thread;

import java.util.ArrayList;
import java.util.List;

public class HappyCinema {
    public static void main(String args[]){
        List<Integer> available=new ArrayList<>();
        for(int i=1;i<10;i++){
            available.add(i);
        }
       Cinema cinema=new Cinema(available,"万达影院");
        List<Integer> seats01=new ArrayList<>();
        List<Integer> seats02=new ArrayList<>();
        seats01.add(1);
        seats01.add(2);
        seats01.add(3);
        seats01.add(4);
        seats02.add(7);
        seats02.add(5);
        seats02.add(6);
        Customer c1=new Customer(cinema,seats01);
        Customer c2=new Customer(cinema,seats02);
        new Thread(c1,"老高").start();
        new Thread(c2,"老田").start();
    }
}
class Cinema{
    List<Integer> available;
    String name;
    public Cinema(List<Integer> available,String name){
        this.available=available;
        this.name=name;
    }
    public boolean bookTickets(List<Integer> seats){
      List<Integer> copy=new ArrayList<>();
      copy.addAll(available);
      copy.removeAll(seats);
        if(available.size()-copy.size()!=seats.size()){
            System.out.println(Thread.currentThread().getName()+"出票失败");
            return false;
        }
        available=copy;
        return true;
    }
}
class Customer implements Runnable{
    Cinema cinema;
    List<Integer> seats;
    public Customer(Cinema cinema,List<Integer> seats){
        this.cinema=cinema;
        this.seats=seats;
    }

    @Override
    public void run() {
        synchronized (cinema) {
            System.out.println("影院剩余的票数" + cinema.available);
            boolean s = cinema.bookTickets(seats);
            if (s) {
                System.out.println(Thread.currentThread().getName() + "购票成功。购票数" + seats + "张");
            }
        }
    }
}
